package com.gemme.gemme.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.util.StrUtil;

import java.util.HashMap;
import java.util.List;

public class ProductDetailView extends RelativeLayout {
    private Context context;
    public ProductDetailView(Context context) {
        this(context,null);
    }

    public ProductDetailView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ProductDetailView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    LinearLayout ll_content;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.product_detail,this);
        ll_content = findViewById(R.id.ll_content);
    }

    public void initData(long id) {
//        id = 25;
        String url = DataManager.BaseUrl_weicheng + "mall/shopproductpicture/findSkuImage?productId="
                + id;
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(getContext()));
//        headMap.put(DataManager.AuthorizationKey,"Bearer 2af9350c-dad4-4e04-b5b9-6a931f9b7b0c");
        DataManager.toServer3(getContext(), url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    ProDetailBean proDetailBean = JSON.parseObject(response, ProDetailBean.class);
                    List<String> data = proDetailBean.getData();
                    System.out.println();
                    setData(data);
                }catch (Exception e){
                    //todo 暂时去除空界面显示
//                    setNUllDataView(true);
                }
            }
        });
    }

    private void setData(List<String> data) {
        try {
            for (int i = 0; i < data.size(); i++) {
                String s = data.get(i);
                ImageView imageView = new ImageView(context);
                if(!StrUtil.isNUll(s)){
                    Glide.with(imageView).load(s).into(imageView);
                    ll_content.addView(imageView);
                }

            }
        }catch (Exception e){

        }
    }
}
